package com.zzyl.test;

import com.alibaba.fastjson2.JSON;
import okhttp3.*;

import java.util.*;

public class TongYi {
    private static final String API_KEY = "sk-a86e8d00d025411bbe6edf35a8f95af4";
    private static final String BASE_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
    private static final String MODEL_NAME = "qwen-plus";

    public static void main(String[] args) {
        try {
            OkHttpClient client = new OkHttpClient();

            // 构建请求参数
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("model", MODEL_NAME);

            Map<String, Object> inputMap = new HashMap<>();
            List<Map<String, String>> messages = new ArrayList<>();

            Map<String, String> systemMessage = new HashMap<>();
            systemMessage.put("role", "system");
            systemMessage.put("content", "You are a helpful assistant.");
            messages.add(systemMessage);

            Map<String, String> userMessage = new HashMap<>();
            userMessage.put("role", "user");
            userMessage.put("content", "你叫卢建鑫现在你和你对象吵架了正在闹分手，你不想和她继续下去了，请说一些伤人的话，达到分手的目的");
            messages.add(userMessage);

            inputMap.put("messages", messages);
            requestMap.put("input", inputMap);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("result_format", "message");
            requestMap.put("parameters", paramMap);

            String jsonParams = JSON.toJSONString(requestMap);
            RequestBody body = RequestBody.create(jsonParams, MediaType.get("application/json; charset=utf-8"));

            Request request = new Request.Builder()
                    .url(BASE_URL)
                    .addHeader("Authorization", "Bearer " + API_KEY)
                    .addHeader("Content-Type", "application/json")
                    .post(body)
                    .build();

            try (Response response = client.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    System.err.println("请求失败，状态码: " + response.code());
                    return;
                }

                if (response.body() != null) {
                    String responseBody = response.body().string();
                    System.out.println("响应内容: " + responseBody);
                }
            }

        } catch (Exception e) {
            System.err.println("错误信息：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
